clear
clear matrix
program drop _all
capture log close
cd C:\Users\dpage\Desktop\

	set more off
	use ESS1through8.dta, clear 
	
*Including Switzerland and Ukraine
replace RainbowIndex = 38.44 if country1==4
replace RainbowIndex = 20.95 if country1==31

*************************************************
*Table 1 
	proportion vote if freehms<3
	proportion vote if freehms>3

    mean Participation if freehms<3
	mean Participation if freehms>3
	
	mean eduyrs if freehms<3
	mean eduyrs if freehms>3
	
	mean agea if freehms<3
	mean agea if freehms>3
	
	mean lrscale if freehms<3
	mean lrscale if freehms>3
*************************************************

	gen edu15 = EU15*eduyrs
	gen free15edu = EU15*freehms*eduyrs 
	*gen free15 = EU15*freehms
	gen freeedu = freehms*eduyrs
	gen lr15 = EU15*lrscale
	
	gen eduRB = RainbowIndex*eduyrs
	gen freeRBedu = RainbowIndex*freehms*eduyrs 
    gen freeRB = RainbowIndex*freehms
	gen lrRB = RainbowIndex*lrscale

		set more off
	
	*Tolerant States
	*Homophobic
	proportion vote if freehms>3 & RainbowIndex>66
	*Tolerant
	proportion vote if freehms<3 & RainbowIndex>66 
	
	*Homophobic States 
	*Homophobic
	proportion vote if freehms>3 & RainbowIndex<25
	*Tolerant
	proportion vote if freehms<3 & RainbowIndex<25 
	
*************************************************
*Table 2

	mixed Participation RainbowIndex freehms freeRB lrscale agea eduyrs [pweight=myweight] ||countryyear:  

	melogit vote RainbowIndex freehms freeRB lrscale agea eduyrs [pweight=myweight] ||countryyear:  
*************************************************

		
		set more off 
			 
		sum lrscale 
			gen LR_mean = r(mean) 
		sum freehms 
			gen Ri_mean = r(mean) 
		sum agea 
			gen Age_mean = r(mean)
		sum eduyrs 
			gen Ed_mean = r(mean)
		
		keep if _n==1 
		keep LR_mean Ri_mean Age_mean Ed_mean 
		 
		matrix def b = e(b)
		matrix def V = e(V)
		mat list b
		mat list V
		
		set more off
		save FirstDifferencesIVs.dta, replace
	
	set more off
		*program drop firstdiff2
	program define firstdiff2
	use FirstDifferencesIVs.dta, clear
	
	drawnorm b1-b8, means(b) cov(V)
		
    ***********************************
	*Feeling sexuality discrimination *
	***********************************
	
	*EU15
		#delimit ;
		gen pParticipation15d = b1*67 + b2*5 + b3*67*5 +
		b4*LR_mean + b5*Age_mean + b6*Ed_mean + b7
		;
		#delimit cr

		#delimit ;
		gen pParticipation15nd = b1*67 + b2*1 + b3*67*1 +
		b4*LR_mean + b5*Age_mean + b6*Ed_mean + b7
		;
		#delimit cr
	
	*New Members 
		#delimit ;
		gen pParticipationEd = b1*24 + b2*5 + b3*5*24 +
		b4*LR_mean + b5*Age_mean + b6*Ed_mean + b7
		;
		#delimit cr

		#delimit ;
		gen pParticipationEnd = b1*24 + b2*1 + b3*24*1 +
		b4*LR_mean + b5*Age_mean + b6*Ed_mean + b7
		;
		#delimit cr
		
		gen Participation15d = exp(pParticipation15d)/(1+exp(pParticipation15d))
		gen Participation15nd = exp(pParticipation15nd)/(1+exp(pParticipation15nd))
		gen ParticipationEd = exp(pParticipationEd)/(1+exp(pParticipationEd))
		gen ParticipationEnd = exp(pParticipationEnd)/(1+exp(pParticipationEnd))
		
		gen DiffEast = ParticipationEd - ParticipationEnd
		gen DiffWest = Participation15d - Participation15nd

		append using FirstDiffs2.dta
			save FirstDiffs2.dta, replace
			
			end
							
			clear
			save FirstDiffs2.dta, replace emptyok
	
			set more off
			//simulating the creation of first differences at difference numbers of experiences
			simulate, reps(1500):  firstdiff2

			use FirstDiffs2.dta, clear

			
						set more off			
#delimit ; 
collapse (mean) 
Participation15d Participation15nd ParticipationEd ParticipationEnd DiffEast DiffWest
 (sd)
 Participation15d_sd=Participation15d Participation15nd_sd=Participation15nd 
 ParticipationEd_sd=ParticipationEd ParticipationEnd_sd=ParticipationEnd
 DiffEast_sd=DiffEast  DiffWest_sd=DiffWest
 ;
#delimit cr 	

 graph set window fontface "Times New Roman"
 
gen Participation15d_hi = Participation15d + 1.96*Participation15d_sd
gen Participation15d_lo = Participation15d - 1.96*Participation15d_sd

gen Participation15nd_hi = Participation15nd + 1.96*Participation15nd_sd
gen Participation15nd_lo = Participation15nd - 1.96*Participation15nd_sd

gen ParticipationEd_hi = ParticipationEd + 1.96*ParticipationEd_sd
gen ParticipationEd_lo = ParticipationEd - 1.96*ParticipationEd_sd

gen ParticipationEnd_hi = ParticipationEnd + 1.96*ParticipationEnd_sd
gen ParticipationEnd_lo = ParticipationEnd - 1.96*ParticipationEnd_sd 

gen DiffEast_hi = DiffEast + 1.96*DiffEast_sd
gen DiffEast_lo = DiffEast - 1.96*DiffEast_sd

gen DiffWest_hi = DiffWest + 1.96*DiffWest_sd
gen DiffWest_lo = DiffWest - 1.96*DiffWest_sd

browse

gen axis_LFS1_B = 1
gen axis_LFS2_B = 2
gen axis_LFS3_B = 3
gen axis_LFS4_B = 4

lab def afmtlf2 1 "Homophobic" 2 "Tolerant" 3 "Homophobic" 4 "Tolerant" 
label val axis_LFS1_B afmtlf2 

#delimit ;
twoway (bar Participation15d axis_LFS1_B, 
			color(black) barwidth(.5))
		(rcap Participation15d_lo Participation15d_hi axis_LFS1_B,
			lcolor(gs10))
		(bar Participation15nd axis_LFS2_B, 
			color(black) barwidth(.5))
		(rcap Participation15nd_lo Participation15nd_hi axis_LFS2_B,
			lcolor(gs10))
		(bar ParticipationEd axis_LFS3_B , 
			color(gs10) barwidth(.5))
		(rcap ParticipationEd_lo ParticipationEd_hi axis_LFS3_B,
			lcolor(black))
		(bar ParticipationEnd axis_LFS4_B, 
			color(gs10) barwidth(.5))
		(rcap ParticipationEnd_lo ParticipationEnd_hi axis_LFS4_B,
			lcolor(black)),
legend(off)
ylabel(.4(.2)1)
ytitle("Probability of voting", size(med))
title("Low Political Homophobia (black) and High Political Homophobia (grey).", size(med))
xlabel(#3,valuelabel angle(horizontal))
name(GPH1, replace)
scheme(s1mono);
#delimit cr	


	    #delimit ;
		twoway (bar DiffWest axis_LFS1_B, color(black)) (rcap DiffWest_hi DiffWest_lo axis_LFS1_B) 
		(bar DiffEast axis_LFS2_B, color(gs10)) (rcap DiffEast_hi DiffEast_lo axis_LFS2_B), 
		scheme(s1mono) ylabel(-.1(.1).1) ytitle("Difference in Pr(Voting)", size(med))
		yline(0)
		title("Difference in Pr(Voting) between" "those who are homophobic and tolerant", size(med))
		xlabel( 1 "Low Political Homophobia" 2 "High Political Homophobia", noticks) 
		name(GPH2, replace)  
		 legend(off)
		;
		#delimit cr

#delimit ;	
	graph combine GPH1 GPH2, rows(2) title("Figure 1: Probability of voting by opinions about" 
"homosexuality by level of political homophobia" "with 95 percent confidence intervals.", size(medsmall)) xsize(5.5) ysize(9)
note("Dependent variable: Voting (1), Not voting (0)." "Data source: European Social Survey 2002-2016", size(medsmall)) scheme(s1mono)
;
#delimit cr
